package com.woniuxy.controller;

import com.github.pagehelper.PageHelper;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.RbacManagerService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RbacManagerController
 * @Description TODO
 * @Author LI.hh
 * @Date 2022/8/19 17:14
 * @Version 1.0
 */
@RequestMapping("/rbacManager")
@RestController
@CrossOrigin
public class RbacManagerController {

    @Autowired
    RbacManagerService rbacManagerService;

    @RequestMapping("/addOne")
    public ResponseEntity addOne(@RequestBody User user){
        List<User> list= findUserByName(user.getAccount());
        if(list.size()!=0){
            return new ResponseEntity(500,"用户名重复",null);
        }else {
            int row = rbacManagerService.addUser(user);
            if (row == 0) {
                return new ResponseEntity(500, "失败", null);
            } else {
                return new ResponseEntity(200, "成功", null);
            }
        }
    }
    @RequiresRoles("管理员6")
    @GetMapping("/selectAll/{currentPage}/{pageSize}")
    public ResponseEntity findAll(@PathVariable("currentPage")int currentPage,@PathVariable("pageSize") int pageSize) {
        return rbacManagerService.selectAll(currentPage,pageSize);
    }
    @RequestMapping("/editOne")
    public ResponseEntity editOne(@RequestBody User user){
        return rbacManagerService.editOne(user);
    }
    @GetMapping("/search/{currentPage}/{pageSize}")
    public ResponseEntity search(@PathVariable("currentPage")int currentPage,@PathVariable("pageSize") int pageSize,@RequestParam String account){
        return rbacManagerService.search(currentPage,pageSize,account);
    }
    @RequestMapping("/deleteById")
    public ResponseEntity deleteById(String id){
        int rows = rbacManagerService.deleteUserById(Integer.parseInt(id));
        if(rows==0){
            return new ResponseEntity(404,"因为不明原因失败了",null);
        }else{
            return new ResponseEntity(200,"成功删除了",null);
        }
    }
    public List<User> findUserByName(String username){
        List<User> list = rbacManagerService.findUserByName(username);
        return list;
    }
}
